package com.example.tallybook;

import static java.security.AccessController.getContext;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.tallybook.adapter.AccountAdapter;
import com.example.tallybook.adapter.RecordPageAdapter;
import com.example.tallybook.db.AccountBean;
import com.example.tallybook.db.DBManager;
import com.example.tallybook.utils.BudgetDialog;
import com.example.tallybook.utils.MoreDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ListView todayLv;//展示今日收支情况的Listview
    //声明数据源
    List<AccountBean> mDatas;
    AccountAdapter adapter;
    int year,month,day;
    View headerView;
    TextView topOutTv,topInTv,topbudgetTv,topConTv;
    Button editBtn;
    ImageButton moreBtn;
    ImageView searchIv,topShowIv;
    SharedPreferences preferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTime();
        initView();
        preferences=getSharedPreferences("budget", Context.MODE_PRIVATE);
        addVHeadeerView();
        mDatas=new ArrayList<>();
        adapter = new AccountAdapter(this, mDatas);
        todayLv.setAdapter(adapter);
    }
    private void addVHeadeerView(){
        headerView=getLayoutInflater().inflate(R.layout.item_mainlv_top,null);
        todayLv.addHeaderView(headerView);
        topOutTv=headerView.findViewById(R.id.item_mainlv_top_tv_out);
        topInTv=headerView.findViewById(R.id.item_mainlv_top_tv_in);
        topbudgetTv=headerView.findViewById(R.id.item_mainlv_top_tv_budget);
        topConTv=headerView.findViewById(R.id.item_mainlv_top_tv_day);
        topShowIv=headerView.findViewById(R.id.item_mainlv_top_iv_hide);

        topbudgetTv.setOnClickListener(this);
        headerView.setOnClickListener(this);
        topShowIv.setOnClickListener(this);
    }
    /**初始化自带的View的方法*/
    private void initView() {
        todayLv=findViewById(R.id.main_lv);
        editBtn=findViewById(R.id.main_btn_edit);
        moreBtn=findViewById(R.id.main_btn_more);
        searchIv=findViewById(R.id.main_btn_search);

        editBtn.setOnClickListener(this);
        moreBtn.setOnClickListener(this);
        searchIv.setOnClickListener(this);
        setLVongClickListener();
    }
//设置ListView地长按事件
    private void setLVongClickListener() {
        todayLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){//点击了头布局
                return false;
            }
                int pos=position-1;
                AccountBean clickBean=mDatas.get(pos);//获取正在点击地这条消息
                int click_id=clickBean.getId();
                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return false;
        }
    });
    }
//弹出是否删除某一条记录的对话框
    private void showDeleteItemDialog(final AccountBean clickBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定要删除这条记录嘛？")
                .setNegativeButton("取消",null)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                    //执行删除操作
                        int id = clickBean.getId();
                        DBManager.deleteItemFromAccounttbById(id);
                        mDatas.remove(clickBean);
                        setTopShow();//改变头布局的内容
                    }
                });
        builder.create().show();//显示对话框
    }


    private void initTime() {
        Calendar calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH)+1;
        day=calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadDBData();
        setTopShow();
    }
    //设置头布局当中文本内容的显示
    private void setTopShow() {
        float incomeOneDay = DBManager.getSumMoneyOneDay(year,month,day,1);
        float outcomeOneDay = DBManager.getSumMoneyOneDay(year,month,day,0);
        String inofOne="今日支出￥"+outcomeOneDay+"今日收入"+incomeOneDay;
        topConTv.setText(inofOne);
        //获取本月收入与支出金额
        float incomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1);
        float outcomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0);
        String inofOnemoth="今月支出￥"+outcomeOneMonth+"今月收入"+incomeOneMonth;
        topInTv.setText("￥"+incomeOneMonth);
        topOutTv.setText("￥"+outcomeOneMonth);
//显示运算剩余
        float bmoney = preferences.getFloat("bmoney", 0);
        if(bmoney==0){
            topbudgetTv.setText("￥ 0");
        }else {
            float syMoney=bmoney-outcomeOneMonth;
            topbudgetTv.setText("￥"+syMoney);
        }
    }

    private void loadDBData() {
        List<AccountBean> list = DBManager.getAccountListOneDayFromAccounttb(year, month, day);
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.main_btn_search:
                Intent it2=new Intent(this,SearchActivity.class);
                startActivity(it2);
                break;
            case R.id.main_btn_edit:
                Intent it1=new Intent(this,RecordActivity.class);
                startActivity(it1);
                break;
            case R.id.main_btn_more:
                MoreDialog moreDialog = new MoreDialog(this);
                moreDialog.show();
                moreDialog.setDialogSize();
                break;
            case R.id.item_mainlv_top_tv_budget:
                showBudgetDialog();
                break;
            case R.id.item_mainlv_top_iv_hide:
                toggleShow();
                break;
        }
        if(view==headerView){
            Intent intent = new Intent();
            intent.setClass(this, MonthChartActivity.class);
            startActivity(intent);//头布局被点击了
        }
    }
    //显示预算设置对话框
    private void showBudgetDialog() {
        BudgetDialog dialog = new BudgetDialog(this);
        dialog.show();
        dialog.setOnEnsureListener(new BudgetDialog.OnEnsureListener() {
            @Override
            public void onEnsure(float money) {
                //将预算金额写入共享参数当中，进行存储
                SharedPreferences.Editor editor=preferences.edit();
                editor.putFloat("bmoney",money);
                editor.commit();
                //计算剩余金额
                float outcaomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0);//支出
                float syMoney=money-outcaomeOneMonth;
                topbudgetTv.setText("￥"+syMoney);
            }
        });
    }

    boolean isShow=true;
    //如果点击眼睛，数据会显示或者隐蔽
    private void toggleShow() {
        if (isShow) {
            PasswordTransformationMethod passwordTransformationMethod = PasswordTransformationMethod.getInstance();
            topInTv.setTransformationMethod(passwordTransformationMethod);//设置隐蔽
            topOutTv.setTransformationMethod(passwordTransformationMethod);//设置隐蔽
            topbudgetTv.setTransformationMethod(passwordTransformationMethod);//设置隐蔽
            topShowIv.setImageResource(R.mipmap.ih_hide);//设置隐蔽
            isShow=false;
        } else {
            HideReturnsTransformationMethod hideReturnsTransformationMethod = HideReturnsTransformationMethod.getInstance();
            topInTv.setTransformationMethod(hideReturnsTransformationMethod);
            topOutTv.setTransformationMethod(hideReturnsTransformationMethod);
            topbudgetTv.setTransformationMethod(hideReturnsTransformationMethod);
            topShowIv.setImageResource(R.mipmap.ih_show);
            isShow=true;
        }
    }

}

